//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.Globalization;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using System.Collections.Generic;
using System;
using Microsoft.TridentWordAddIn.ResourceHelper;

namespace Microsoft.TridentWordAddIn.Presentation
{
    public partial class DocumentContentsUserControl : UserControl
    {
        private List<ContentItem> contentItems;
        private NumberFormatInfo infoNoDecimals = CultureInfo.InvariantCulture.NumberFormat.Clone() as NumberFormatInfo;
        private NumberFormatInfo infoTwoDecimals = CultureInfo.InvariantCulture.NumberFormat.Clone() as NumberFormatInfo;

        /// <summary>
        /// Initializes a new instance of the <see cref="DocumentContentsUserControl"/> class.
        /// </summary>
        public DocumentContentsUserControl()
        {
            InitializeComponent();

            this.contentItems = new List<ContentItem>();
            this.infoNoDecimals.NumberDecimalDigits = 0;
            this.infoTwoDecimals.NumberDecimalDigits = 2;
        }

        /// <summary>
        /// Gets or sets the content items.
        /// </summary>
        /// <value>The content items.</value>
        public ContentItem[] ContentItems
        {
            get 
            { 
                return this.contentItems.ToArray(); 
            }
            set
            {
                this.contentItems.Clear();
                if (value != null)
                {
                    this.contentItems.AddRange(value);
                }

                this.OnContentListUpdated();
            }
        }

        #region Private Method

        /// <summary>
        /// Converts the size to kb.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <returns>decimal</returns>
        private static decimal ConvertSizeToKb(long size)
        {
            return Math.Ceiling(Convert.ToDecimal(size) / Convert.ToDecimal(1024));
        }

        /// <summary>
        /// Called when [content list updated].
        /// </summary>
        private void OnContentListUpdated()
        {
            this.lvDocumentContents.BeginUpdate();
            this.lvDocumentContents.Items.Clear();
            long totalSize = 0;
            if (this.contentItems != null)
            {
                string[] columns = new string[] { "Name", "Belongs to Association", "Size (KB)" };
                this.lvDocumentContents.AddColumns(columns);
                foreach (ContentItem contentItem in this.contentItems)
                {
                    ListViewItem listViewItem = this.lvDocumentContents.Items.Add(contentItem.Name);
                    switch (contentItem.ContentType)
                    {
                        case ContentItemType.Workflow:
                            listViewItem.ImageKey = "workflow";
                            break;
                        case ContentItemType.Activity:
                            listViewItem.ImageKey = "activity";
                            break;
                        case ContentItemType.ResultFile:
                            listViewItem.ImageKey = "resultFile";
                            break;
                    }

                    listViewItem.SubItems.Add(string.Join(", ", contentItem.BelongsTo.ToArray()));

                    decimal sizeInKb = ConvertSizeToKb(contentItem.Size);
                    listViewItem.SubItems.Add(sizeInKb.ToString("N", this.infoNoDecimals));
                    totalSize += contentItem.Size;
                }
            }

            this.ShowNoDocumentContent();
            this.SetContentSize(totalSize);
            this.lvDocumentContents.EndUpdate();
        }

        /// <summary>
        /// Sets the size of the content.
        /// </summary>
        /// <param name="totalSize">The total size.</param>
        private void SetContentSize(long totalSize)
        {
            if (totalSize > 0)
            {
                this.lblTotalSize.Visible = true;
                this.lblSize.Visible = true;

                double totalSizeMB = Convert.ToDouble(totalSize) / Convert.ToDouble(1024 * 1024);
                this.lblSize.Text = totalSizeMB.ToString("F", this.infoTwoDecimals) + " MB";
            }
            else
            {
                this.lblTotalSize.Visible = false;
                this.lblSize.Visible = false;
            }
        }

        /// <summary>
        /// Shows the content of the no document.
        /// </summary>
        private void ShowNoDocumentContent()
        {
            if (this.lvDocumentContents.Items.Count == 0)
            {
                this.lvDocumentContents.ShowErrorMessage(ResourceFetcher.GetString("NoEmbeddedWorkflows"));
            }
        }

        #endregion
    }
}
